<template>
  <div class="echarts-box">
    <div id="myEcharts" :style="{ width: width, height: height }"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { onMounted, onUnmounted } from "vue";

export default {
  name: "App",
  props: ["width", "height","barTitle"],
  setup(props) {
    let myEcharts = echarts;

    onMounted(() => {
      initChart();
    });

    onUnmounted(() => {
      myEcharts.dispose;
    });

    function initChart() {
      let chart = myEcharts.init(
        document.getElementById("myEcharts"),
        "purple-passion"
      );
      chart.setOption({
        title: {
          show: true,
          text: props.barTitle, //主标题文本
          textStyle: {
            color: "black", //'red'，字体颜色
            fontStyle: "normal", //'italic'(倾斜) | 'oblique'(倾斜体) ，字体风格
            fontWeight: "bold", //'bold'(粗体) | 'bolder'(粗体) | 'lighter'(正常粗细) ，字体粗细
            fontFamily: "sans-serif", //'sans-serif' | 'serif' | 'monospace' | 'Arial' | 'Courier New'
            // 'Microsoft YaHei'(微软雅黑) ，文字字体
            fontSize: "0.1rem", //字体大小
            lineHeight: 15, //字体行高
          },
          textAlign: "auto", //整体（包括 text 和 subtext）的水平对齐
          textVerticalAlign: "auto", //整体（包括 text 和 subtext）的垂直对齐
          padding: 0, //[5,10] | [ 5,6, 7, 8] ,标题内边距
          left: "4%", //'5' | '5%'，title 组件离容器左侧的距离
          right: "auto", //'title 组件离容器右侧的距离
          top: "2%", //title 组件离容器上侧的距离
          bottom: "auto", //title 组件离容器下侧的距离
        },
        legend: {
          data: ["所有评分"],
        },
        xAxis: {
          type: "category",
          data: [
            "60分以下",
            "60分-70分",
            "70分-80分",
            "80分-90分",
            "90分-100分",
          ],
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis",
        },
        series: [
          {
            name: "所有评分",
            label: {
              show: true,
              formatter: "{c}人",
              position: "top",
              color: "rgb(80,135,236)",
            },
            itemStyle: {
              color: "rgb(80,135,236)",
            },
            data: [100, 140, 230, 100, 70],
            type: "bar",
          },
        ],
      });
      window.onresize = function () {
        chart.resize();
      };
    }

    return {
      initChart,
    };
  },
};
</script>
